using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace HIT.DAL
{
	/// <summary>
	/// Strongly-typed collection for the Item class.
	/// </summary>
    [Serializable]
	public partial class ItemCollection : ActiveList<Item, ItemCollection>
	{	   
		public ItemCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>ItemCollection</returns>
		public ItemCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                Item o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the Item table.
	/// </summary>
	[Serializable]
	public partial class Item : ActiveRecord<Item>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Item()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public Item(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public Item(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public Item(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Item", TableType.Table, DataService.GetInstance("HITProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarItemId = new TableSchema.TableColumn(schema);
				colvarItemId.ColumnName = "ItemId";
				colvarItemId.DataType = DbType.Int32;
				colvarItemId.MaxLength = 0;
				colvarItemId.AutoIncrement = true;
				colvarItemId.IsNullable = false;
				colvarItemId.IsPrimaryKey = true;
				colvarItemId.IsForeignKey = false;
				colvarItemId.IsReadOnly = false;
				colvarItemId.DefaultSetting = @"";
				colvarItemId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarItemId);
				
				TableSchema.TableColumn colvarItemName = new TableSchema.TableColumn(schema);
				colvarItemName.ColumnName = "ItemName";
				colvarItemName.DataType = DbType.String;
				colvarItemName.MaxLength = -1;
				colvarItemName.AutoIncrement = false;
				colvarItemName.IsNullable = false;
				colvarItemName.IsPrimaryKey = false;
				colvarItemName.IsForeignKey = false;
				colvarItemName.IsReadOnly = false;
				colvarItemName.DefaultSetting = @"";
				colvarItemName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarItemName);
				
				TableSchema.TableColumn colvarDescription = new TableSchema.TableColumn(schema);
				colvarDescription.ColumnName = "Description";
				colvarDescription.DataType = DbType.AnsiString;
				colvarDescription.MaxLength = 2147483647;
				colvarDescription.AutoIncrement = false;
				colvarDescription.IsNullable = true;
				colvarDescription.IsPrimaryKey = false;
				colvarDescription.IsForeignKey = false;
				colvarDescription.IsReadOnly = false;
				colvarDescription.DefaultSetting = @"";
				colvarDescription.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDescription);
				
				TableSchema.TableColumn colvarThumbnail = new TableSchema.TableColumn(schema);
				colvarThumbnail.ColumnName = "Thumbnail";
				colvarThumbnail.DataType = DbType.Binary;
				colvarThumbnail.MaxLength = 2147483647;
				colvarThumbnail.AutoIncrement = false;
				colvarThumbnail.IsNullable = true;
				colvarThumbnail.IsPrimaryKey = false;
				colvarThumbnail.IsForeignKey = false;
				colvarThumbnail.IsReadOnly = false;
				colvarThumbnail.DefaultSetting = @"";
				colvarThumbnail.ForeignKeyTableName = "";
				schema.Columns.Add(colvarThumbnail);
				
				TableSchema.TableColumn colvarOriginal = new TableSchema.TableColumn(schema);
				colvarOriginal.ColumnName = "Original";
				colvarOriginal.DataType = DbType.Binary;
				colvarOriginal.MaxLength = 2147483647;
				colvarOriginal.AutoIncrement = false;
				colvarOriginal.IsNullable = true;
				colvarOriginal.IsPrimaryKey = false;
				colvarOriginal.IsForeignKey = false;
				colvarOriginal.IsReadOnly = false;
				colvarOriginal.DefaultSetting = @"";
				colvarOriginal.ForeignKeyTableName = "";
				schema.Columns.Add(colvarOriginal);
				
				TableSchema.TableColumn colvarValueX = new TableSchema.TableColumn(schema);
				colvarValueX.ColumnName = "Value";
				colvarValueX.DataType = DbType.Currency;
				colvarValueX.MaxLength = 0;
				colvarValueX.AutoIncrement = false;
				colvarValueX.IsNullable = true;
				colvarValueX.IsPrimaryKey = false;
				colvarValueX.IsForeignKey = false;
				colvarValueX.IsReadOnly = false;
				colvarValueX.DefaultSetting = @"";
				colvarValueX.ForeignKeyTableName = "";
				schema.Columns.Add(colvarValueX);
				
				TableSchema.TableColumn colvarSerialNumber = new TableSchema.TableColumn(schema);
				colvarSerialNumber.ColumnName = "SerialNumber";
				colvarSerialNumber.DataType = DbType.String;
				colvarSerialNumber.MaxLength = -1;
				colvarSerialNumber.AutoIncrement = false;
				colvarSerialNumber.IsNullable = true;
				colvarSerialNumber.IsPrimaryKey = false;
				colvarSerialNumber.IsForeignKey = false;
				colvarSerialNumber.IsReadOnly = false;
				colvarSerialNumber.DefaultSetting = @"";
				colvarSerialNumber.ForeignKeyTableName = "";
				schema.Columns.Add(colvarSerialNumber);
				
				TableSchema.TableColumn colvarModel = new TableSchema.TableColumn(schema);
				colvarModel.ColumnName = "Model";
				colvarModel.DataType = DbType.String;
				colvarModel.MaxLength = -1;
				colvarModel.AutoIncrement = false;
				colvarModel.IsNullable = true;
				colvarModel.IsPrimaryKey = false;
				colvarModel.IsForeignKey = false;
				colvarModel.IsReadOnly = false;
				colvarModel.DefaultSetting = @"";
				colvarModel.ForeignKeyTableName = "";
				schema.Columns.Add(colvarModel);
				
				TableSchema.TableColumn colvarManufacturer = new TableSchema.TableColumn(schema);
				colvarManufacturer.ColumnName = "Manufacturer";
				colvarManufacturer.DataType = DbType.String;
				colvarManufacturer.MaxLength = -1;
				colvarManufacturer.AutoIncrement = false;
				colvarManufacturer.IsNullable = true;
				colvarManufacturer.IsPrimaryKey = false;
				colvarManufacturer.IsForeignKey = false;
				colvarManufacturer.IsReadOnly = false;
				colvarManufacturer.DefaultSetting = @"";
				colvarManufacturer.ForeignKeyTableName = "";
				schema.Columns.Add(colvarManufacturer);
				
				TableSchema.TableColumn colvarRoomId = new TableSchema.TableColumn(schema);
				colvarRoomId.ColumnName = "RoomId";
				colvarRoomId.DataType = DbType.Int32;
				colvarRoomId.MaxLength = 0;
				colvarRoomId.AutoIncrement = false;
				colvarRoomId.IsNullable = false;
				colvarRoomId.IsPrimaryKey = false;
				colvarRoomId.IsForeignKey = true;
				colvarRoomId.IsReadOnly = false;
				colvarRoomId.DefaultSetting = @"";
				
					colvarRoomId.ForeignKeyTableName = "Room";
				schema.Columns.Add(colvarRoomId);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["HITProvider"].AddSchema("Item",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("ItemId")]
		[Bindable(true)]
		public int ItemId 
		{
			get { return GetColumnValue<int>(Columns.ItemId); }
			set { SetColumnValue(Columns.ItemId, value); }
		}
		  
		[XmlAttribute("ItemName")]
		[Bindable(true)]
		public string ItemName 
		{
			get { return GetColumnValue<string>(Columns.ItemName); }
			set { SetColumnValue(Columns.ItemName, value); }
		}
		  
		[XmlAttribute("Description")]
		[Bindable(true)]
		public string Description 
		{
			get { return GetColumnValue<string>(Columns.Description); }
			set { SetColumnValue(Columns.Description, value); }
		}
		  
		[XmlAttribute("Thumbnail")]
		[Bindable(true)]
		public byte[] Thumbnail 
		{
			get { return GetColumnValue<byte[]>(Columns.Thumbnail); }
			set { SetColumnValue(Columns.Thumbnail, value); }
		}
		  
		[XmlAttribute("Original")]
		[Bindable(true)]
		public byte[] Original 
		{
			get { return GetColumnValue<byte[]>(Columns.Original); }
			set { SetColumnValue(Columns.Original, value); }
		}
		  
		[XmlAttribute("ValueX")]
		[Bindable(true)]
		public decimal? ValueX 
		{
			get { return GetColumnValue<decimal?>(Columns.ValueX); }
			set { SetColumnValue(Columns.ValueX, value); }
		}
		  
		[XmlAttribute("SerialNumber")]
		[Bindable(true)]
		public string SerialNumber 
		{
			get { return GetColumnValue<string>(Columns.SerialNumber); }
			set { SetColumnValue(Columns.SerialNumber, value); }
		}
		  
		[XmlAttribute("Model")]
		[Bindable(true)]
		public string Model 
		{
			get { return GetColumnValue<string>(Columns.Model); }
			set { SetColumnValue(Columns.Model, value); }
		}
		  
		[XmlAttribute("Manufacturer")]
		[Bindable(true)]
		public string Manufacturer 
		{
			get { return GetColumnValue<string>(Columns.Manufacturer); }
			set { SetColumnValue(Columns.Manufacturer, value); }
		}
		  
		[XmlAttribute("RoomId")]
		[Bindable(true)]
		public int RoomId 
		{
			get { return GetColumnValue<int>(Columns.RoomId); }
			set { SetColumnValue(Columns.RoomId, value); }
		}
		
		#endregion
		
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Room ActiveRecord object related to this Item
		/// 
		/// </summary>
		public HIT.DAL.Room Room
		{
			get { return HIT.DAL.Room.FetchByID(this.RoomId); }
			set { SetColumnValue("RoomId", value.RoomId); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varItemName,string varDescription,byte[] varThumbnail,byte[] varOriginal,decimal? varValueX,string varSerialNumber,string varModel,string varManufacturer,int varRoomId)
		{
			Item item = new Item();
			
			item.ItemName = varItemName;
			
			item.Description = varDescription;
			
			item.Thumbnail = varThumbnail;
			
			item.Original = varOriginal;
			
			item.ValueX = varValueX;
			
			item.SerialNumber = varSerialNumber;
			
			item.Model = varModel;
			
			item.Manufacturer = varManufacturer;
			
			item.RoomId = varRoomId;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varItemId,string varItemName,string varDescription,byte[] varThumbnail,byte[] varOriginal,decimal? varValueX,string varSerialNumber,string varModel,string varManufacturer,int varRoomId)
		{
			Item item = new Item();
			
				item.ItemId = varItemId;
			
				item.ItemName = varItemName;
			
				item.Description = varDescription;
			
				item.Thumbnail = varThumbnail;
			
				item.Original = varOriginal;
			
				item.ValueX = varValueX;
			
				item.SerialNumber = varSerialNumber;
			
				item.Model = varModel;
			
				item.Manufacturer = varManufacturer;
			
				item.RoomId = varRoomId;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn ItemIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn ItemNameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn DescriptionColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn ThumbnailColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn OriginalColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn ValueXColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn SerialNumberColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        public static TableSchema.TableColumn ModelColumn
        {
            get { return Schema.Columns[7]; }
        }
        
        
        
        public static TableSchema.TableColumn ManufacturerColumn
        {
            get { return Schema.Columns[8]; }
        }
        
        
        
        public static TableSchema.TableColumn RoomIdColumn
        {
            get { return Schema.Columns[9]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string ItemId = @"ItemId";
			 public static string ItemName = @"ItemName";
			 public static string Description = @"Description";
			 public static string Thumbnail = @"Thumbnail";
			 public static string Original = @"Original";
			 public static string ValueX = @"Value";
			 public static string SerialNumber = @"SerialNumber";
			 public static string Model = @"Model";
			 public static string Manufacturer = @"Manufacturer";
			 public static string RoomId = @"RoomId";
						
		}
		#endregion
		
		#region Update PK Collections
		
        #endregion
    
        #region Deep Save
		
        #endregion
	}
}
